// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import 'babel-polyfill'
import promise from 'es6-promise';
promise.polyfill();
import Vue from 'vue'
import axios from 'axios'
import VueAxios from 'vue-axios'
import qs from 'qs'
import LocalStore from 'store'
import ElementUI, {Message} from 'element-ui'
import './assets/scss/element-variables.scss'
import './assets/scss/reset-element.scss'
import strSubLeaveOut from './filters/global_strSubLeaveOut'
import strAddKilobit from './filters/global_addKilobit'
import App from './App'
import router from './router'
import backButton from './components/backButton.vue'
import HPDialogTip from "@/components/HPDialogTip";

Vue.config.productionTip = false
axios.defaults.baseURL = BASE_URL;
Vue.filter(strSubLeaveOut.id, strSubLeaveOut.filterFn);
Vue.filter(strAddKilobit.id, strAddKilobit.filterFn);
axios.defaults.headers['Access-Token'] = LocalStore.get('token');
axios.defaults.transformRequest = function (data) {
    let tempItem = '',
        tempData = []

    const type = Object.prototype.toString.call(data).slice(8, -1).toLowerCase()
    if(type === 'formdata') {
        return data;
    }
    for (let item in data) {
        tempItem = data[item];
        if (data[item] && typeof tempItem === 'string') {
            tempData[item] = tempItem.replace(/^\s+|\s+$/g, '')
        } else {
            tempData[item] = tempItem
        }
    }
    let datas = qs.stringify(tempData)
    console.log(datas)
    return datas;
}
axios.interceptors.request.use(
    config => {
        console.log(config.url)
        return config
    }
)
axios.interceptors.response.use(function (response) {
    let callBackStatus = response.data;
    if (callBackStatus.state === '1004') {
        LocalStore.set('token', '');
        axios.defaults.headers['Access-Token'] = '';
        router.push('/login')
    }else if(callBackStatus.state && callBackStatus.state !== '0000'){
      Message.error(callBackStatus.message)
    }
    return response
})
router.beforeEach((to, form, next) => {
    let t = LocalStore.get('t');
    let token = LocalStore.get('token');
    let nowt = new Date().getTime();
    // let minute = Math.floor((nowt - t) / (60 * 1000))
    // console.log(minute)
    if ((!token || token === '' || token === undefined) && to.name !== 'Login') {
        router.push('/login')
    } else {
        next();
    }
    // else if ((minute >= 30 && to.name !== 'Login')) {
    //     LocalStore.set('token', '')
    //     axios.defaults.headers['Access-Token'] = '';
    //     router.push('/login')
    // } 
    
})
Vue.prototype.$message = Message
Vue.use(VueAxios, axios);
Vue.use(ElementUI)
Vue.component('HPDialogTip', HPDialogTip)
Vue.component('backButton', backButton)
/* eslint-disable no-new */
new Vue({
    el: '#app',
    router,
    template: '<App/>',
    components: { App }
})
